﻿using H2.NET.Common;
using H2.NET.Web.BI.Model;
using H2.NET.Web.Workflow.DwzCode;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace H2.NET.Web.BI.Template
{
    public partial class Index : BasePage
    {
        [SessionType]
        public clsCommon clsCmm = new clsCommon();
        public string name;
        public List<bi_Template> templates = new List<bi_Template>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (AksRequest.GetQueryString("action") == "delete")
            {
                using (var cxt = new UltimusMDC3Entities())
                {
                    int tid = AksRequest.GetFormInt("id");
                    if (AksRequest.GetFormInt("id") > 0)
                    {
                        var template = cxt.bi_Template.Find(tid);
                        if (clsCmm.comm_username != template.create_user)
                        {
                            ErrorMsg("对不起，您没有权限删除此模版！");
                            return;
                        }
                        cxt.bi_Template.Remove(template);
                        cxt.SaveChanges();
                    }
                }

                SuccessMsg("AccountIndex");
                return;
            }
            var context = new UltimusMDC3Entities();
            PageNumber = Convert.ToInt32(Request.Form["pageNum"] ?? "1");
            Pager1.PageSize = PageSize;
            Pager1.PageNumber = PageNumber;
            name = Request.Form["name"] ?? "";
            string currentUser = clsCmm.comm_username;
            string strSQL = " select b.grp_m_fk_grp_no as groupNo";
            strSQL += "  from t_group_member b";
            strSQL += " where b.status = 1 and b.grp_m_type_v = '" + currentUser + "'";
            strSQL += " order by b.grp_m_type";
            clsCmm.clsDB.Storage("oleDB");
            DataTable dt = clsCmm.clsDB.Query(strSQL);
            List<string> nos = new List<string>();
            foreach (DataRow n in dt.Rows)
            {
                if (n.ItemArray.Count() > 0)
                {
                    nos.Add(n.ItemArray[0].ToString());
                }
            }
            templates = context.bi_Template.Where(t => t.name.Contains(name) && (t.create_user == clsCmm.comm_username || ((t.pub==true&&nos.Where(n => t.permission.IndexOf(n + ",") >= 0).Count() > 0))))
                .OrderByDescending(t => t.id).Skip((PageNumber - 1) * PageSize).Take(PageSize).ToList();
            TotalItemCount = templates.Count();
            Pager1.TotalItemCount = TotalItemCount;
        }
    }
}